﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMVConvert;

namespace WMVEditor
{
	public partial class MainForm : Form
	{
		private WMVConvert.Metadata.Editor medataEditor;

		public MainForm(string fileName)
		{
			InitializeComponent();
			if (!string.IsNullOrEmpty(fileName))
			{
				Open(fileName);
			}
		}

		private void ReBindData()
		{
			if (medataEditor != null)
			{
				//this.bindingWMEditor.DataSource = medataEditor.Attributes;
				this.bindingWMAttributes.DataSource = medataEditor.Attributes;
				if (medataEditor.Attributes.WMPicture.Value != null)
				{
					this.bindingWMPictureValue.DataSource = medataEditor.Attributes.WMPicture.Value;
				}
			}
			else
			{
				this.bindingWMEditor.DataSource = System.Linq.Enumerable.Empty<WMVConvert.Metadata.WMAttribute>();
				this.pictureBox.Image = null;
			}
		}

		private void Open(string fileName)
		{
			if (medataEditor != null)
			{
				medataEditor.Close();
				medataEditor = null;
			}
			medataEditor = new WMVConvert.Metadata.Editor(fileName);
			ReBindData();
			this.Text = fileName;
			this.saveToolStripMenuItem.Enabled = true;
		}

		private void Save()
		{
			try
			{
				toolStripStatusLabel.Text = Properties.Resources.Saving;
				if (medataEditor != null)
				{
					medataEditor.Save();
				}
				toolStripStatusLabel.Text = Properties.Resources.Ready;
			}
			catch (Exception e)
			{
				toolStripStatusLabel.Text = e.Message;
				throw;
			}
		}

		private void CloseFile()
		{
			if (medataEditor != null)
			{
				medataEditor.Close();
				medataEditor.Dispose();
				medataEditor = null;
				ReBindData();
				this.saveToolStripMenuItem.Enabled = false;
				ComponentResourceManager resources = new ComponentResourceManager(GetType());
				resources.ApplyResources(this, "$this");
			}
		}

		private void openToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.openFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				Open(this.openFileDialog.FileName);
			}
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Close();
			Application.Exit();
		}

		private void saveToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Save();
		}

		private void toolStripMenuItemClose_Click(object sender, EventArgs e)
		{
			CloseFile();
		}

		private void linkPoster_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (openPictureDialog.ShowDialog() == DialogResult.OK)
			{
				medataEditor.Attributes.WMPicture.Value =
					new WMVConvert.Metadata.WMPicture(openPictureDialog.FileName);
			}
		}

		private void toolStripMenuItemPaste_Click(object sender, EventArgs e)
		{
			if (Clipboard.ContainsImage())
			{
				medataEditor.Attributes.WMPicture.Value =
					new WMVConvert.Metadata.WMPicture(Clipboard.GetImage());
			}
		}
	}
}
